package cn.ltgodm.moj.message;

import cn.ltgodm.moj.service.JudgeService;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author ltgodm
 * @date 2024-03-14 21:46:42
 */
@Component
@Slf4j
public class JudgeMessageConsumer {

    @Resource
    JudgeService judgeService;

    @SneakyThrows
    @RabbitListener(queues = {"code_queue"},ackMode = "MANUAL")
    public void receiveMessage(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) Long deliveryTag){
        log.info("receiveMessage:{}",message);
        Long submitQuestionId = Long.valueOf(message);
        try {
            judgeService.doJudge(submitQuestionId);
            channel.basicAck(deliveryTag,false);
        }catch (Exception e){
            log.error("消息队列执行失败",e);
        }
    }
}
